<?php
namespace app\index\model;

use app\common\model\City;
use think\Model;

class Brand extends Model{

    private $brandModel;
    public function __construct(){
        parent::__construct();
        $this -> brandModel = db('brand');
    }

    /*  品牌详情
     *  @param Array [where] int type
     *  @return Array [data]
     * **/
    public function getBrandDetailByWhere($where,$type=1){
        $data = $this -> brandModel -> where($where) -> find();
        if(empty($data)){   return [];  }
        switch ($type) {
            case 1://品牌百科
                $dataList = $this -> formatBrandDataForDetail($data);
                break;

            default:
                # code...
                break;
        }
        return $dataList;
    }
    /*  获取品牌数据分页
     *  @param Array [where]
     *  @return Array [dataList]
     * **/
    public function getBrandListForPageByWhere($where,$order = false,$num=10,&$page,&$cateIdArr){
        $brandData = $this -> brandModel -> where($where) -> order($order) -> paginate($num,false,['query' => $where]);
        $page = $brandData->render();
        $data = iterator_to_array($brandData);
        if(empty($data)){   return [];  }
        $dataList = $this -> formatBrandDataForList($data,$cateIdArr);
        return $dataList;

    }


    /*  获取品牌数据
     *  @param Array [where]
     *  @return Array [dataList]
     * **/
    public function getBrandDataList($where,$order = false ,$type = 1,$limit = false , &$redun = false ){
        $data = $this -> brandModel -> where($where) -> order($order) -> limit($limit) -> select();
        if(empty($data)){   return [];  }
        switch ($type) {
            case 1:
                $dataList = $this -> formarBrandListForSubtitleId($data);
                break;
            case 2:
                $dataList = $this -> formatBrandListForIndex($data);
                break;
            case 3:
                $dataList = $this -> formatBrandDataForList($data,$redun);
                break;

            default:
                # code...
                break;
        }
        return $dataList;
    }

    /*  格式化数据（项目名/项目id）
     *  @param Array [data]
     *  @return Array [list]
     * **/
    public function formarBrandListForSubtitleId($data){

        foreach ($data as $key => $value) {
            $list[$key]['title'] = $value['title'];
            $list[$key]['id'] = $value['id'];
        }
        return $list;
    }
    /*  格式化数据(品牌名、封面图、人气、特色)
     *  @param Array [data]
     *  @return Array [list]
     * **/
    public function formatBrandListForIndex($data){
        foreach ($data as $key => $value) {
            $list[$key]['id'] = $value['id'];
            $list[$key]['title'] = $value['title'];
            $list[$key]['img'] = $value['img'];
            $list[$key]['read_num'] = $value['read_num'];
            $list[$key]['ame'] = $value['ame'];
        }
        return $list;
    }
    /*  品牌百科
     *  @param Array [data]
     *  @return Array [info]
     * **/
    public function formatBrandDataForDetail($data){
        //加盟区域
//        dump($data);die;
        if($data['province'] == 1){
            $data['city'] = '全国';
        }else{
            $cityModel = new City();
            $city = $cityModel -> getCityValue(['id'=>$data['province']],'cityname');
            if($data['city'] !== 1 && !empty($data['city'])){
                $city .= $cityModel -> getCityValue(['id'=>$data['city']],'cityname');
            }
            $data['city'] = $city;
        }
        //图集
        $data['image'] = explode(',',trim($data['image'],','));
        return $data;
    }
    /*  品牌列表
     *  @param Array [data]
     *  @return Array [list]
     * **/
    public static function formatBrandDataForList($data,&$cateIdArr){
        foreach ($data as $key => $value) {
            $cateIdArr[] = $value['cate_pid'];
            $list[$key]['id'] = $value['id'];
            $list[$key]['title'] = $value['title'];
            $list[$key]['subtitle'] = $value['subtitle'];
            $list[$key]['img'] = $value['img'];
            $list[$key]['sign'] = $value['sign'];
        }
        return $list;
    }
    /*  执行浏览量+1
     *  @param Array [where]
     *  @return Array [result]
     * **/
    public function fieldAddOne($where,$value){
        $retult = $this -> brandModel -> where($where) -> setInc($value);
        return $retult;
    }


}